package com.jincheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jincheng.system.domain.CommentLike;
import com.jincheng.system.mapper.CommentLikeMapper;
import com.jincheng.system.service.ICommentLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class CommentLikeServiceImpl implements ICommentLikeService {

    private final CommentLikeMapper commentLikeMapper;


    /**
     * 查询评论点赞记录
     *
     * @param commentLike 查询条件
     * @return 结果
     */
    @Override
    public List<CommentLike> list(CommentLike commentLike) {
        LambdaQueryWrapper<CommentLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(commentLike.getCommentId() != null,
                CommentLike::getCommentId, commentLike.getCommentId());
        wrapper.eq(commentLike.getUserId() != null,
                CommentLike::getUserId, commentLike.getUserId());
        wrapper.eq(commentLike.getLikeId() != null,
                CommentLike::getLikeId, commentLike.getLikeId());
        return commentLikeMapper.selectList(wrapper);
    }

    /**
     * 新增或修改评论点赞
     *
     * @param commentLike 评论点赞信息
     * @return 结果
     */
    @Override
    public int saveOrUpdate(CommentLike commentLike) {
        Long count = commentLikeMapper.selectCount(new LambdaQueryWrapper<CommentLike>()
                .eq(CommentLike::getCommentId, commentLike.getCommentId())
                .eq(CommentLike::getUserId, commentLike.getUserId())
        );
        int rows;
        if (count > 0) {
            rows = commentLikeMapper.update(new LambdaUpdateWrapper<CommentLike>()
                    .eq(CommentLike::getCommentId, commentLike.getCommentId())
                    .eq(CommentLike::getUserId, commentLike.getUserId())
                    .set(CommentLike::getIsLike, commentLike.getIsLike())
            );
        } else {
            rows = commentLikeMapper.insert(commentLike);
        }
        return rows;
    }
}
